package tirtilcam.model;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.URLConnection;
import java.util.Hashtable;

public class Utils
{

    public static boolean m_streamEnd = false;

    public static final String BOUNDARY_MARKER_PREFIX = "--";

    private DataInputStream m_dis = null;

    public Utils(DataInputStream p_dis)
    {
	m_dis = p_dis;
    }

    @SuppressWarnings("unchecked")
    public Hashtable readHeaders() throws IOException
    {
	Hashtable ht = new Hashtable();
	String response = null;
	boolean satisfied = false;

	do
	{
	    response = m_dis.readLine();
	    if (response == null)
	    {
		m_streamEnd = true;
		break;
	    } else if (response.equals(""))
	    {
		if (satisfied)
		{
		    break;
		} else
		{
		    // Carry on...
		}
	    } else
	    {
		satisfied = true;
	    }
	    // addPropValue(response, ht);
	} while (true);
	return ht;
    }

    public static Hashtable readHttpHeaders(URLConnection conn)
    {
	Hashtable ht = new Hashtable();
	boolean foundAny = false;
	int i = 0;
	do
	{
	    String key = conn.getHeaderFieldKey(i);
	    if (key == null)
	    {
		if (i == 0)
		{
		    i++;
		    continue;
		} else
		{
		    break;
		}
	    }
	    String val = conn.getHeaderField(i);
	    ht.put(key.toLowerCase(), val);
	    i++;
	} while (true);
	return ht;
    }

    public byte[] readToBoundary(String boundary) throws IOException
    {
	ResizableByteArrayOutputStream baos = new ResizableByteArrayOutputStream();
	StringBuffer lastLine = new StringBuffer();
	int lineidx = 0;
	int chidx = 0;
	byte ch;
	do
	{
	    try
	    {
		ch = m_dis.readByte();
	    } catch (EOFException e)
	    {
		m_streamEnd = true;
		break;
	    }
	    if (ch == '\n' || ch == '\r')
	    {
		//
		// End of line...
		//
		String lls = lastLine.toString();
		if (lls.startsWith(BOUNDARY_MARKER_PREFIX))
		{
		    if (lls.startsWith(boundary))
		    {
			//
			// Boundary found - check for termination
			//
			String btest = lls.substring(boundary.length());
			if (btest.equals(boundary))
			{
			    m_streamEnd = true;
			}
			chidx = lineidx;
			break;
		    }
		}
		lastLine = new StringBuffer();
		lineidx = chidx + 1;
	    } else
	    {
		lastLine.append((char) ch);
	    }
	    chidx++;
	    baos.write(ch);
	} while (true);
	//
	baos.close();
	baos.resize(chidx);
	return baos.toByteArray();
    }

    class ResizableByteArrayOutputStream extends ByteArrayOutputStream
    {
	public ResizableByteArrayOutputStream()
	{
	    super();
	}

	public void resize(int size)
	{
	    count = size;
	}
    }

    public static BufferedImage resizeImage(BufferedImage image, int width,
	    int height)
    {
	BufferedImage resizedImage = new BufferedImage(width, height,
		BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(image, 0, 0, width, height, null);
	g.dispose();
	return resizedImage;
    }

}
